<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改小程序省市区')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-city-edit" th:object="${jiajiaCity}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">省份：</label>
                <div class="col-sm-8">
                    <select name="provinceId" class="form-control m-b" th:onchange="provinceChange(this)" id="provinceId">
                        <option value="">---请选择---</option>
                        <option th:each="province : ${provinceList}" th:value="${province.id}" th:text="${province.name}" th:selected="${province.id eq jiajiaCity.provinceId}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">城市：</label>
                <div class="col-sm-8">
                    <select name="cityId" class="form-control m-b" id="cityId">
                        <option th:each="city : ${cityList}" th:value="${city.id}" th:text="${city.name}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text">
                </div>
            </div>
           
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "jiajia/city";
        $("#form-city-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-city-edit').serialize());
            }
        }

        function provinceChange(obj){
            getCity(obj.value)
        }

        function getCity(pid){
            var cityId = /*[[${jiajiaCity.cityId}]]*/ '';
            $("#cityId").html("")
            $.post(prefix + "/district/list", {"pid":pid}, function (data){
                var htmlStr = "";
                data.rows.forEach((rowObj, index, array) => {
                    htmlStr = htmlStr + "<option value = '"+rowObj.id+"'";
                    if(rowObj.id==cityId){
                        htmlStr = htmlStr + " selected='true'";
                    }
                        htmlStr = htmlStr + ">"+rowObj.name+"</option>"
                })
                $("#cityId").html(htmlStr)
            });
        }

        $(function (){
            var provinceId = /*[[${jiajiaCity.provinceId}]]*/ '';
            if(provinceId != null && provinceId != ''){
                getCity(provinceId)
            }
        })
    </script>
</body>
</html>